package com.shjn.squirrel.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.shjn.squirrel.R;
import com.shjn.squirrel.model.UserInfoCache;
import com.shjn.squirrel.utils.MeasureScreenUtils;
import com.shjn.squirrel.customView.BackAndTimerView;

/**
 * 大件回收
 */
public class BigRecoveryFragment extends BaseFragment {

    private static BigRecoveryFragment instance;

    public BigRecoveryFragment() {
    }

    public synchronized static BigRecoveryFragment getInstance() {
        if (instance == null) {
            instance = new BigRecoveryFragment();
        }
        return instance;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return createView(inflater, R.layout.big_piece_recovery, container);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        initTimer();
    }

    /**
     * 初始化倒计时器
     */
    private void initTimer() {
        //加载定时器面板，设置定时时间
        mActivity.mBackAndTimer.setOnBackListener(new BackAndTimerView.OnBackListener() {
            @Override
            public void onBack() {
                //返回首页
                if (!UserSelectFragment.getInstance().isAdded() && !BottomFragment.getInstance().isAdded()) {
                    backHomePage();
                }
            }
        }).setBackVisibleState(true).setOnTimerFinishListener(new BackAndTimerView.OnTimerFinishListener() {
            @Override
            public void onTimerFinish() {
                //返回到首页
                if (!UserSelectFragment.getInstance().isAdded() && !BottomFragment.getInstance().isAdded()) {
                    backHomePage();
                }
            }
        }).setTime(60).setVisibleState(true).start();
    }

    /**
     * 返回首页
     */
    private void backHomePage() {
        mActivity.mBackAndTimer.setVisibleState(false);
        mActivity.mBackAndTimer.setY(97 * MeasureScreenUtils.getDensity(mActivity));
        mActivity.getFragmentManager().beginTransaction()
                .remove(BigRecoveryFragment.this)
                .replace(R.id.mid, UserSelectFragment.getInstance())
                .replace(R.id.bottom, BottomFragment.getInstance())
                .commit();
        if (!UserInfoCache.loginState) {
            TopFragment.getInstance().mBtnMyRecycler.setVisibility(View.VISIBLE);
        }
    }
}
